import { _decorator, Component, Node, Label, Vec3, v3, UITransform } from 'cc';
const { ccclass, property } = _decorator;

import SSS_TimerManager from "../../../Script/Manager/SSS_TimerManager";
import { SpiderSolitaire_GameData } from './SpiderSolitaire_GameData';
import SpiderSolitaire_Card from './SpiderSolitaire_Card';

@ccclass('SpiderSolitaire_Grid')
export default class SpiderSolitaire_Grid extends Component {
    @property(Node)
    red: Node | null = null;
    @property(Label)
    Label_pos: Label | null = null;
    col: number = 0;
    row: number = 0;
    id: number = 0;
    block: SpiderSolitaire_Card = null;
    //    // 初始化
    init(_col: number, _row: number, _id: number) {
        this.col = _col;
        this.row = _row;
        this.id = _id;

        this.block = null;

        this.InitWidthAndHeight(SpiderSolitaire_GameData.size, SpiderSolitaire_GameData.size);
        this.InitPosition();
        this.Refresh();
    }
    Refresh() {
        this.Label_pos.string = `col-${this.col}\nrow-${this.row}`;
        this.Label_pos.node.active = false;
    }
    GetNodeWorldPostation(_node: Node): Vec3 {
        let pos: Vec3 = v3();
        if (_node.parent == null) {
            pos = v3(_node.getPosition());
        } else {
            // pos = v3(_node.parent.convertToWorldSpaceAR(_node.getPosition()));
            pos = _node.getWorldPosition();
        }
        return pos;
    }
    //    // 初始化单元宽高
    InitWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }
    //    // 初始化位置
    InitPosition() {
        let x = -SpiderSolitaire_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * (this.col - 1);
        let y = -SpiderSolitaire_GameData.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * (this.row - 1);
        this.node.position = v3(x, y, 0);
    }
    async Flash() {
        let duration = 0.2;
        this.red.active = true;
        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = false;

        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = true;
        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = false;

        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = true;
        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = false;
    }
}